package com.intelligent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.intelligent.mapper.ReceivableDetailMapper;
import com.intelligent.pojo.ReceivableDetail;
import com.intelligent.service.ReceivableDetailService;
import com.intelligent.vo.ReceivableDetailPageReq;
import com.intelligent.vo.ReceivableDetailResp;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.intelligent.mapper.SalesOrdersMapper;
import com.intelligent.mapper.CustomerManagementMapper;
import com.intelligent.mapper.CustomerInfoMapper;
import com.intelligent.pojo.SalesOrders;
import com.intelligent.pojo.CustomerManagement;
import com.intelligent.pojo.CustomerInfo;

import java.util.stream.Collectors;

@Service
public class ReceivableDetailServiceImpl extends ServiceImpl<ReceivableDetailMapper, ReceivableDetail>
    implements ReceivableDetailService{

    @Resource
    private SalesOrdersMapper salesOrdersMapper;

    @Resource
    private CustomerManagementMapper customerManagementMapper;

    @Resource
    private CustomerInfoMapper customerInfoMapper;

    public boolean create(ReceivableDetail detail) {
        if (detail.getReceivableAmount() != null && detail.getReceivedAmount() != null) {
            detail.setUnreceivedAmount(detail.getReceivableAmount().subtract(detail.getReceivedAmount()));
        }
        return this.save(detail);
    }

    public Page<ReceivableDetailResp> page(ReceivableDetailPageReq req) {
        Page<ReceivableDetail> entityPage = new Page<>(req.getPageNum(), req.getPageSize());
        LambdaQueryWrapper<ReceivableDetail> qw = new LambdaQueryWrapper<>();
        if (req.getCustomerId() != null) qw.eq(ReceivableDetail::getCustomerId, req.getCustomerId());
        if (req.getOrderId() != null) qw.eq(ReceivableDetail::getOrderId, req.getOrderId());
        if (req.getReconciliationId() != null) qw.eq(ReceivableDetail::getReconciliationId, req.getReconciliationId());
        if (req.getReceivableType() != null) qw.eq(ReceivableDetail::getReceivableType, req.getReceivableType());
        if (req.getStatus() != null) qw.eq(ReceivableDetail::getStatus, req.getStatus());
        if (req.getSettlementType() != null) qw.eq(ReceivableDetail::getSettlementType, req.getSettlementType());
        qw.orderByDesc(ReceivableDetail::getId);
        this.page(entityPage, qw);

        Page<ReceivableDetailResp> voPage = new Page<>(entityPage.getCurrent(), entityPage.getSize(), entityPage.getTotal());
        voPage.setRecords(entityPage.getRecords().stream().map(this::toResp).collect(Collectors.toList()));
        return voPage;
    }

    public ReceivableDetailResp detail(Long id) {
        ReceivableDetail d = this.getById(id);
        return d == null ? null : toResp(d);
    }

    private ReceivableDetailResp toResp(ReceivableDetail d) {
        ReceivableDetailResp r = new ReceivableDetailResp();
        r.setId(d.getId());
        r.setCustomerId(d.getCustomerId());
        if (d.getCustomerId() != null) {
            // 兼容两种外键：
            // 1) receivable_detail.customer_id 指向 customer_management.id
            CustomerManagement cm = customerManagementMapper.selectById(d.getCustomerId());
            if (cm != null) {
                r.setCustomerName(cm.getCustomerName());
            } else {
                // 2) receivable_detail.customer_id 指向 customer_info.id，再由 customer_info.customerId -> customer_management.id
                CustomerInfo ci = customerInfoMapper.selectById(d.getCustomerId());
                if (ci != null && ci.getCustomerId() != null) {
                    CustomerManagement cm2 = customerManagementMapper.selectById(ci.getCustomerId());
                    if (cm2 != null) {
                        r.setCustomerName(cm2.getCustomerName());
                    }
                }
            }
        }
        r.setOrderId(d.getOrderId());
        if (d.getOrderId() != null) {
            SalesOrders so = salesOrdersMapper.selectById(d.getOrderId());
            if (so != null) {
                r.setOrderNo(so.getOrderNo());
            }
        }
        r.setReconciliationId(d.getReconciliationId());
        r.setReceivableType(d.getReceivableType());
        r.setReceivableAmount(d.getReceivableAmount());
        r.setReceivedAmount(d.getReceivedAmount());
        r.setUnreceivedAmount(d.getUnreceivedAmount());
        r.setDueDate(d.getDueDate());
        r.setStatus(d.getStatus());
        r.setPaymentTerm(d.getPaymentTerm());
        r.setSettlementType(d.getSettlementType());
        r.setRemark(d.getRemark());
        r.setCreateTime(d.getCreateTime());
        return r;
    }
}
